package com.vshop.framework.web.autoconfig.i18n;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.LocaleResolver;

import java.util.Locale;

/**
 * 获取请求头国际化信息
 */
public class I18nLocaleResolver implements LocaleResolver {

    public static final String ContentLanguageHeaderKey = "content-language";

    @Override
    public Locale resolveLocale(@NonNull HttpServletRequest request) {
        String language = request.getHeader(ContentLanguageHeaderKey);
        Locale locale = Locale.getDefault();
        if (language != null && !language.isEmpty()) {
            String[] split = language.split("_");
//            locale = new Locale(split[0], split[1]);
            locale = Locale.of(split[0], split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(@Nullable HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
